﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Calculator
{
    public partial class Calculator : System.Web.UI.Page
    {
        int first;
        int second;
        protected void add_Click(object sender, EventArgs e) 
        {
            GetNumbers(out first, out second);

            this.result.Text = (first + second).ToString();
        }

        protected void substract_Click(object sender, EventArgs e)
        {
            GetNumbers(out first, out second);
            this.result.Text = (first - second).ToString();
        }

        protected void multiply_Click(object sender, EventArgs e) 
        {
            GetNumbers(out first, out second);
            this.result.Text = (first * second).ToString();
        }

        protected void divide_Click(object sender, EventArgs e)
        {
            GetNumbers(out first, out second);
            CheckForZeros();

            this.result.Text = ((double)first /(double) second).ToString();
        }

        protected void root_Click(object sender, EventArgs e) 
        {
            GetNumbers(out first, out second);
            CheckForZeros();
            double power = ((double)1/(double)second);
            this.result.Text = (Math.Pow((double)first,power)).ToString();
        }

        protected void module_Click(object sender, EventArgs e) 
        {
            GetNumbers(out first, out second);
            CheckForZeros();
            this.result.Text = (first % second).ToString();
        }

        private void CheckForZeros()
        {
            if (second == 0)
            {
                throw new DivideByZeroException("Emergency you`re creating a black hole");
            }
        }

        private void GetNumbers( out int firstNumber,out  int secondNumber)
        {
            firstNumber = ParseNumber(this.fistNumber.Text);
            secondNumber = ParseNumber(this.secondNumber.Text);
        }

        private int ParseNumber(string numberAsString)
        {
            return int.Parse(numberAsString);
        }
    }
}